package com.irdstudio.efp.esb.api.service.psd;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.threadpool.ApplicationThreadPool2;
import com.irdstudio.basic.framework.core.util.DateTool;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.facade.PtProductUserService;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.console.service.vo.PtProductUserVO;
import com.irdstudio.efp.esb.api.bean.psd.precredit.req.NlsPsdPreCreditApprReqBean;
import com.irdstudio.efp.esb.api.bean.psd.precredit.resp.NlsPsdPreCreditApprRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.MsLoanConstant;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.req.EsbReqSysHeadBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.service.bo.req.psd.PsdPreCreditApprReqBean;
import com.irdstudio.efp.esb.service.bo.resp.psd.PsdPreCreditApprRespBean;
import com.irdstudio.efp.esb.service.facade.EsbInvokeWrapper;
import com.irdstudio.efp.esb.service.facade.psd.PsdPreCreditApprService;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;
import com.irdstudio.efp.limit.service.vo.LmtPrdContVO;
import com.irdstudio.efp.nls.common.constant.PrdInfoEnum;
import com.irdstudio.efp.nls.common.constant.sed.SedNlsEnum;
import com.irdstudio.efp.nls.service.facade.NlsQueueSoltComnService;
import com.irdstudio.efp.nls.service.facade.PsdSendTaxRecordService;
import com.irdstudio.efp.nls.service.facade.psd.PreNlsCreditInfoService;
import com.irdstudio.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.irdstudio.efp.nls.service.vo.PsdSendTaxRecordVO;
import com.irdstudio.efp.nls.service.vo.psd.PreNlsCreditInfoVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static com.irdstudio.efp.console.common.ConsoleConstant.LIMIT_STS_02;

/**
 * 需求编号：【2019D1218】（普税贷需求）
 * 问题编号：【2019D1218】（普税贷预授信申请）
 * 开发人员：【ludayong】
 * 创建日期：【2019/12/17 10:05】
 * 功能描述：
 * 修改日期：【2019/12/17 10:05】
 * 修改描述：
 */
@Service("nlsPsdPreCreditProcessServcie")
public class NlsPsdPreCreditProcessServcie implements FrameworkService {

    /**
     * 需求编号：【2019D1218】（普税贷需求）
     * 问题编号：【2019D1218】（普税贷预授信申请处理流程）
     * 开发人员：【ludayong】
     * 创建日期：【2019/12/17 10:05】
     * 功能描述：
     * 修改日期：【2019/12/17 10:05】
     * 修改描述：
     */
    public EsbRespServiceBean processPreCreditApply(NlsPsdPreCreditApprReqBean nlsPsdPreCreditApprReqBean, EsbReqServiceBean esbReqServiceBean) throws Exception {
        // 预授信申请基本信息校验
        validateReqInfo(nlsPsdPreCreditApprReqBean, esbReqServiceBean);

        // 新增预授信申请信息
        insertPreCreditInfo(nlsPsdPreCreditApprReqBean, esbReqServiceBean);

        // 新增税局回写记录 如果改预授信三天内没有被授信阶段回写 或者没有发生授信 则需要回写失败
        insertSendToTaxRecord(nlsPsdPreCreditApprReqBean, esbReqServiceBean);

        // 调用大数据风控预授信审批接口
        invokePreCreditAppr(nlsPsdPreCreditApprReqBean);

        // 调用大数据风控预授信审批接口返回信息
        NlsPsdPreCreditApprRespBean nlsPsdPreCreditApprRespBean = new NlsPsdPreCreditApprRespBean();
        nlsPsdPreCreditApprRespBean.setRetCd(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE());
        nlsPsdPreCreditApprRespBean.setRetMsg("交易成功");

        EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean, nlsPsdPreCreditApprRespBean);
        // 清除TraceUtil信息
        TraceUtil.clear();
        return esbRespServiceBean;
    }

    /**
     * 需求编号：【2019D1218】（普税贷需求）
     * 问题编号：【2019D1218】（新增预授信申请信息）
     * 开发人员：【ludayong】
     * 创建日期：【2019/12/17 10:05】
     * 功能描述：
     * 修改日期：【2019/12/17 10:05】
     * 修改描述：
     */
    private void insertPreCreditInfo(NlsPsdPreCreditApprReqBean nlsPsdPreCreditApprReqBean, EsbReqServiceBean esbReqServiceBean) throws Exception {
        // 全局流水号
        String glblSrlNo = Optional.ofNullable(esbReqServiceBean.getSysHead()).orElseGet(EsbReqSysHeadBean::new).getGlblSrlNo();
        // 渠道申请号
        String chanAppNo = nlsPsdPreCreditApprReqBean.getChanAppNo();
        // 预授信申请信息
        PreNlsCreditInfoVO preNlsCreditInfoVO = new PreNlsCreditInfoVO();
        // 预授信申请流水号
        preNlsCreditInfoVO.setApplySeq(chanAppNo);
        // 渠道申请号
        preNlsCreditInfoVO.setChannelApplyNo(chanAppNo);
        // 产品编码
        preNlsCreditInfoVO.setPrdId(nlsPsdPreCreditApprReqBean.getPdCd());
        // 纳税识别号
        preNlsCreditInfoVO.setTaxIdentificationNumber(nlsPsdPreCreditApprReqBean.getTaxpyrIdntfNo());
        // 企业注册地
        preNlsCreditInfoVO.setRegistration(nlsPsdPreCreditApprReqBean.getEntpRgstAddr());
        // 推荐人工号
        preNlsCreditInfoVO.setRecommenderAccount(nlsPsdPreCreditApprReqBean.getRefrId());
        // 推荐人所属机构
        if (!StringUtil.isNullorBank(nlsPsdPreCreditApprReqBean.getRefrId())) {
            // 客户经理
            PtProductUserVO queryPtProductUserVO = ptProductUserService.getPtProduceByLoginName(nlsPsdPreCreditApprReqBean.getRefrId());
            if (Objects.nonNull(queryPtProductUserVO)) {
                // 推荐人所属机构
                preNlsCreditInfoVO.setRecommenderOrgid(queryPtProductUserVO.getDeptId());
                // 业务经办人
                preNlsCreditInfoVO.setNlsOperUserid(queryPtProductUserVO.getLoginName());
                // 业务经办机构
                preNlsCreditInfoVO.setNlsOperOrgid(queryPtProductUserVO.getDeptId());
            }
        }
        // 审批状态
        preNlsCreditInfoVO.setStatus(MsLoanConstant.NlsApplyStateEnum.APPR.getVALUE());
        // 创建人
        preNlsCreditInfoVO.setCreateUser(BaseConstant.ADMIN);
        // 创建日期
        preNlsCreditInfoVO.setCreateDate(DateTool.getCurrentDate());
        // 创建时间
        preNlsCreditInfoVO.setCreateTime(DateTool.getCurrentDateTime());
        // 全局流水号
        preNlsCreditInfoVO.setGlobalSerno(glblSrlNo);
        // 推荐人工号(渠道端信息)
        preNlsCreditInfoVO.setRecommenderId(nlsPsdPreCreditApprReqBean.getRefrId());
        // 三个月重复申请标识
        preNlsCreditInfoVO.setRepAppFlag(SedNlsEnum.YesOrNo.YES.getValue());
        PreNlsCreditInfoVO queryPreNlsCreditInfo = preNlsCreditInfoService.queryByChannelApplyNo(preNlsCreditInfoVO);
        if (queryPreNlsCreditInfo == null) {
            int num = preNlsCreditInfoService.insertPreNlsCreditInfo(preNlsCreditInfoVO);
            if (num == -1) {
                String msg = "预授信申请信息【" + chanAppNo + "】" + "，新增预授信申请信息失败！";
                logger.error(msg);
                throw new BizException(EsbRetCodeStandard.SJCLYC.getValue(), "新增预授信申请信息失败！");
            }
        } else {
            // 最后更新人
            preNlsCreditInfoVO.setLastUpdateUser(BaseConstant.ADMIN);
            // 最后更新时间
            preNlsCreditInfoVO.setLastUpdateTime(DateTool.getCurrentDateTime());
            int num = preNlsCreditInfoService.updateByPk(preNlsCreditInfoVO);
            if (num == -1) {
                String msg = "预授信申请信息【" + chanAppNo + "】" + "，更新预授信申请信息失败！";
                logger.error(msg);
                throw new BizException(EsbRetCodeStandard.SJCLYC.getValue(), "更新预授信申请信息失败！");
            }
        }
    }

    /**
     * 添加授信回写记录
     * @param nlsPsdPreCreditApprReqBean
     * @param esbReqServiceBean
     * @throws Exception
     */
    private void insertSendToTaxRecord(NlsPsdPreCreditApprReqBean nlsPsdPreCreditApprReqBean, EsbReqServiceBean esbReqServiceBean) throws Exception {
        try {

            //根据纳税识别号判断七天内是否存在
            List<PsdSendTaxRecordVO> psdSendTaxRecordVOS = psdSendTaxRecordService.queryRecordByTaxnoAndSendType(nlsPsdPreCreditApprReqBean.getTaxpyrIdntfNo(), "0");
            if (!psdSendTaxRecordVOS.isEmpty()) {
                PsdSendTaxRecordVO psdSendTaxRecordVO = psdSendTaxRecordVOS.get(0);
                Date createTime = psdSendTaxRecordVO.getCreateTime();
                long l = (new Date().getTime() - createTime.getTime()) / (1000 * 3600 * 24);
                if (l<=maxSendDay) {
                    return;
                }
            }
            PsdSendTaxRecordVO psdSendTaxRecordVO = new PsdSendTaxRecordVO();
            psdSendTaxRecordVO.setTaxNo(nlsPsdPreCreditApprReqBean.getTaxpyrIdntfNo());
            psdSendTaxRecordVO.setApplySeq(nlsPsdPreCreditApprReqBean.getChanAppNo());
            psdSendTaxRecordVO.setApplyStatus("N");
            psdSendTaxRecordVO.setApplyMsg("纳税人授信失败");
            psdSendTaxRecordVO.setSendType("0");
            psdSendTaxRecordVO.setBizDate(pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001));
            psdSendTaxRecordVO.setHandStatus("N");
            psdSendTaxRecordVO.setHandNum(0);
            psdSendTaxRecordVO.setCreateTime(new Date());
            psdSendTaxRecordService.insert(psdSendTaxRecordVO);
        } catch (Exception ex) {
            logger.error("预授信阶段回写插入回写税局表发生异常",ex);
        }
    }

    /**
     * 需求编号：【2019D1218】（普税贷需求）
     * 问题编号：【2019D1218】（调用大数据分控预授信审批接口）
     * 开发人员：【ludayong】
     * 创建日期：【2019/12/16 19:51】
     * 功能描述：
     * 修改日期：【2019/12/16 19:51】
     * 修改描述：
     */
    private void invokePreCreditAppr(NlsPsdPreCreditApprReqBean nlsPsdPreCreditApprReqBean) throws Exception {
        // 预授信申请信息
        PreNlsCreditInfoVO preNlsCreditInfoVO = new PreNlsCreditInfoVO();
        // 渠道申请号
        preNlsCreditInfoVO.setChannelApplyNo(nlsPsdPreCreditApprReqBean.getChanAppNo());
        // 预授信申请信息
        PreNlsCreditInfoVO queryPreNlsCreditInfo = Optional.ofNullable(preNlsCreditInfoService.queryByChannelApplyNo(preNlsCreditInfoVO)).orElseGet(PreNlsCreditInfoVO::new);
        // 产品信息
        PrdInfoVO prdInfo = new PrdInfoVO();
        prdInfo.setPrdCode(nlsPsdPreCreditApprReqBean.getPdCd());
        PrdInfoVO queryPrdInfo = Optional.ofNullable(prdInfoService.queryLastPrdInfo(prdInfo)).orElseGet(PrdInfoVO::new);

        // 普税贷预核额审批请求信息
        PsdPreCreditApprReqBean psdPreCreditApprReqBean = new PsdPreCreditApprReqBean();
        PsdPreCreditApprReqBean.QryRptsInfArry qryRptsInf = new PsdPreCreditApprReqBean.QryRptsInfArry();
        // AppNo	申请编号
        qryRptsInf.setAppNo(nlsPsdPreCreditApprReqBean.getChanAppNo());
        // TaxpyrIdntfNo	纳税人识别号
        qryRptsInf.setTaxpyrIdntfNo(nlsPsdPreCreditApprReqBean.getTaxpyrIdntfNo());
        // EntpLo	企业所在地
        qryRptsInf.setEntpLo(nlsPsdPreCreditApprReqBean.getEntpRgstAddr());
        // UsrID	用户ID
        qryRptsInf.setUsrID(ESBClientConstance.CnsmrSysNo);
        // DeptNo	部门号
        qryRptsInf.setDeptNo(queryPreNlsCreditInfo.getNlsOperOrgid());
        // OCMPLSExstdCrLmt	网贷已有授信额度
        //申请人网贷及个贷已有授信额度:同一个证件号的所有信用担保类型的有效的授信额度之和
        LmtPrdContVO lmtPrdContVO = new LmtPrdContVO();
        lmtPrdContVO.setCertType(preNlsCreditInfoVO.getCertType());
        lmtPrdContVO.setCertCode(preNlsCreditInfoVO.getCertNo());
        lmtPrdContVO.setLmtStatus(LIMIT_STS_02);
        lmtPrdContVO.setAssureMeansMain(BaseConstant.ASSURE_MEANS_00);
        List<LmtPrdContVO> lmtPrdContVOS = lmtPrdContService.aplcOCMExstdCrLmt(lmtPrdContVO);
        BigDecimal approveAmtTotal = lmtPrdContVOS.stream().
                map(LmtPrdContVO::getLmtAmt).
                filter(Objects::nonNull).
                reduce(BigDecimal.ZERO, BigDecimal::add);
        qryRptsInf.setOCMPLSExstdCrLmt(approveAmtTotal.doubleValue());
        // 执行利率
        Double loanIntRate = Objects.isNull(queryPrdInfo.getLoanIntRate()) ? 0.0 : queryPrdInfo.getLoanIntRate().doubleValue();
        // AnulzLoanIntRt	年化贷款利率
        qryRptsInf.setAnulzLoanIntRt(loanIntRate);
        // 查询报告信息数组
        PsdPreCreditApprReqBean.QryRptsInfArry[] qryRptsInfArry = {qryRptsInf};
        psdPreCreditApprReqBean.setQryRptsInfArry(qryRptsInfArry);
        // 全局流水号
        psdPreCreditApprReqBean.setGolSeq(queryPreNlsCreditInfo.getGlobalSerno());

        // 发送审批通知请求
        EsbInvokeWrapper esbInvokeWrapper = new EsbInvokeWrapper(psdPreCreditApprService, psdPreCreditApprReqBean, nlsQueueSoltComnService);
        threadPool.add(() -> {
            try {
                PsdPreCreditApprRespBean psdPreCreditApprRespBean = Optional.ofNullable((PsdPreCreditApprRespBean) esbInvokeWrapper.invoke3Threes()).orElseGet(PsdPreCreditApprRespBean::new);
                if (Objects.isNull(psdPreCreditApprRespBean.getQryRptsInfArry()) || psdPreCreditApprRespBean.getQryRptsInfArry().length < 1
                        || !EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(psdPreCreditApprRespBean.getQryRptsInfArry()[0].getRcvSt())) {
                    throw new RuntimeException("调用大数据风控预核额审批接口异常！");
                }
            } catch (Exception e) {
                updatePreNlsCreditInfo(queryPreNlsCreditInfo, e, EsbRetCodeStandard.OTHERS_EXCEPTION);
                logger.error(nlsPsdPreCreditApprReqBean.getChanAppNo() + "------调用大数据风控预授信审批接口异常！");
                e.printStackTrace();
            }
        });
    }

    /**
     * 需求编号：【2019D1218】（普税贷需求）
     * 问题编号：【2019D1218】（预授信申请信息规则校验）
     * 开发人员：【ludayong】
     * 创建日期：【2019/12/17 9:35】
     * 功能描述：
     * 修改日期：【2019/12/17 9:35】
     * 修改描述：
     */
    private void validateReqInfo(NlsPsdPreCreditApprReqBean nlsPsdPreCreditApprReqBean, EsbReqServiceBean esbReqServiceBean) throws Exception {
        // 渠道申请号
        String chanAppNo = nlsPsdPreCreditApprReqBean.getChanAppNo();
        // 纳税识别号
        String taxpyrIdntfNo = nlsPsdPreCreditApprReqBean.getTaxpyrIdntfNo();
        // 产品编码
        String prdId = nlsPsdPreCreditApprReqBean.getPdCd();

        // 产品信息校验
        PrdInfoVO prdInfo = new PrdInfoVO();
        prdInfo.setPrdCode(prdId);
        PrdInfoVO queryPrdInfo = prdInfoService.queryLastPrdInfo(prdInfo);

        if (Objects.isNull(queryPrdInfo) || !PrdInfoEnum.PSD.getPrdId().equals(queryPrdInfo.getPrdId())) {
            String msg = "产品信息校验不通过！";
            logger.error(msg);
            throw new BizException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品信息校验不通过！");
        }

        PreNlsCreditInfoVO inPreCreditByTaxpyrIdntfNo = new PreNlsCreditInfoVO();
        // 纳税识别号
        inPreCreditByTaxpyrIdntfNo.setTaxIdentificationNumber(taxpyrIdntfNo);
        // 产品编号
        inPreCreditByTaxpyrIdntfNo.setPrdId(PrdInfoEnum.PSD.getPrdId());
        // 通过纳税识别号，产品编号查询预授信授信申请信息
        List<PreNlsCreditInfoVO> preCreditByTaxpyrIdntfNoList = Optional.ofNullable(preNlsCreditInfoService.queryByCondition(inPreCreditByTaxpyrIdntfNo)).orElseGet(ArrayList::new);

        // 根据纳税识别编号查询预授信信息，存在处理中的预授信，如果存在，则拒绝
        int handleNums = preCreditByTaxpyrIdntfNoList.stream().map(PreNlsCreditInfoVO::getStatus).filter(Objects::nonNull).filter(t -> {
            return t.equals(MsLoanConstant.NlsApplyStateEnum.APPR.getVALUE()) || t.equals(MsLoanConstant.NlsApplyStateEnum.HANG.getVALUE());
        }).mapToInt(e -> 1).sum();
        if (handleNums > 0) {
            String msg = "存在关联处理中的预授信！";
            logger.error(msg);
            throw new BizException(msg);
        }

        // 根据纳税识别号和产品编号查询预授信信息关联的授信信息是否存在处理中的授信，如果存在，则拒绝
        // 根据纳税识别号和产品编号查询预授信信息关联的授信信息的额度信息额度状态不为已失效，拒绝本次预授信申请
        for (PreNlsCreditInfoVO preNlsCreditInfo : preCreditByTaxpyrIdntfNoList) {
            // 根据纳税识别号判断近90天是否存在审批拒绝的记录
            //判断时间相差90天内
            if (LocalDate.now().toEpochDay()-LocalDate.parse(preNlsCreditInfo.getCreateDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).toEpochDay() <= 90) {
                if ("N".equals(preNlsCreditInfo.getApprovalResultMark())) {
                    throw new BizException("三个月内无法重新发起预核额申请。");
                }
            }
            // 通过预授信申请编号查询授信申请信息
            List<NlsCreditInfoVO> preLmtAppSeqList = Optional.ofNullable(nlsCreditInfoService.queryByPreLmtApplySeq(preNlsCreditInfo.getApplySeq())).orElseGet(ArrayList::new);

            // 根据纳税识别号判断是否存在处理中、通过状态的授信授信申请记录，如果存在则拒绝
            for (NlsCreditInfoVO nlsCreditInfoVO : preLmtAppSeqList) {
                if ("01".equals(nlsCreditInfoVO.getApprvSts())) {
                     throw new BizException("您已经存在处理中的授信申请记录，暂时无法重新发起预核额申请。");
                }
                if ("02".equals(nlsCreditInfoVO.getApprvSts())) {
                    throw new BizException("您已经存在审批状态为通过的授信申请记录，暂时无法重新发起预核额申请。");
                }
            }

            for (NlsCreditInfoVO nlsCreditInfoVO : preLmtAppSeqList) {
                LmtPrdContVO inLmtPrdContVO = new LmtPrdContVO();
                // 授信申请流水号
                inLmtPrdContVO.setLmtApplySeq(nlsCreditInfoVO.getLmtApplySeq());
                // 查询授信协议信息
                LmtPrdContVO queryLmtPrdContVO = lmtPrdContService.queryByPk(inLmtPrdContVO);
                if (Objects.nonNull(queryLmtPrdContVO) && ConsoleConstant.LIMIT_STS_02.equals(queryLmtPrdContVO.getLmtStatus())) {
                    DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
                    String msg = "您的授信额度为<"+decimalFormat.format(queryLmtPrdContVO.getLmtAmt())+">，可用授信额度为<"+decimalFormat.format(queryLmtPrdContVO.getAvailAmt())+">，授信终止日为<"+queryLmtPrdContVO.getExpireDate()+">。您可直接访问“我要提款”进行提款。";
                    logger.error(msg);
                    throw new BizException(msg);
                }
            }

            for (NlsCreditInfoVO queryNlsCreditByPreLmtAppSeq : preLmtAppSeqList) {
                // 存在处理中、挂起的授信
                if (MsLoanConstant.NlsApplyStateEnum.APPR.getVALUE().equals(queryNlsCreditByPreLmtAppSeq.getApprvSts())
                        || MsLoanConstant.NlsApplyStateEnum.HANG.getVALUE().equals(queryNlsCreditByPreLmtAppSeq.getApprvSts())) {
                    String msg = "存在关联处理中的授信！";
                    logger.error(msg);
                    throw new BizException(msg);
                } else {
                    // 授信关联的额度状态不为已失效，拒绝本次预授信申请
                    // 授信协议信息
                    LmtPrdContVO inLmtPrdContVO = new LmtPrdContVO();
                    // 授信申请流水号
                    inLmtPrdContVO.setLmtApplySeq(queryNlsCreditByPreLmtAppSeq.getLmtApplySeq());
                    // 查询授信协议信息
                    LmtPrdContVO queryLmtPrdContVO = lmtPrdContService.queryByPk(inLmtPrdContVO);
                    if (Objects.nonNull(queryLmtPrdContVO) && !ConsoleConstant.LIMIT_STS_03.equals(queryLmtPrdContVO.getLmtStatus())) {
                        String msg = "存在关联额度信息非失效状态！";
                        logger.error(msg);
                        throw new BizException(msg);
                    }
                }
            }
        }
    }

    /**
     * 需求编号：【2019D1218】（普税贷需求）
     * 问题编号：【2019D1218】（审批失败更新预授信申请信息）
     * 开发人员：【ludayong】
     * 创建日期：【2019/12/16 16:19】
     * 功能描述：
     * 修改日期：【2019/12/16 16:19】
     * 修改描述：
     */
    public void updatePreNlsCreditInfo(PreNlsCreditInfoVO queryPreNlsCreditInfo, Exception exception, EsbRetCodeStandard esbRetCodeStandard) {
        PreNlsCreditInfoVO inPreNlsCreditInfoVo = new PreNlsCreditInfoVO();
        // 渠道申请号
        String chanAppNo = queryPreNlsCreditInfo.getChannelApplyNo();
        // 预授信申请流水号
        inPreNlsCreditInfoVo.setApplySeq(chanAppNo);
        // 渠道申请号
        inPreNlsCreditInfoVo.setChannelApplyNo(chanAppNo);
        // 审批状态
        inPreNlsCreditInfoVo.setStatus(MsLoanConstant.NlsApplyStateEnum.REJECT.getVALUE());
        // 拒绝原因
        inPreNlsCreditInfoVo.setRefuseReason(exception.getLocalizedMessage());
        // 拒绝码
        inPreNlsCreditInfoVo.setRefuseCode(esbRetCodeStandard.getValue());
        // 审批时间
        inPreNlsCreditInfoVo.setApprovalTime(DateTool.getCurrentDateTime());
        // 最后更新人
        inPreNlsCreditInfoVo.setLastUpdateUser(BaseConstant.ADMIN);
        // 最后更新时间
        inPreNlsCreditInfoVo.setLastUpdateTime(DateTool.getCurrentDateTime());
        // 三个月重复申请标识
        inPreNlsCreditInfoVo.setRepAppFlag(SedNlsEnum.YesOrNo.NO.getValue());
        // 更预授信申请信息
        preNlsCreditInfoService.updateByPk(inPreNlsCreditInfoVo);
    }

    // 日志信息
    private static final Logger logger = LoggerFactory.getLogger(NlsPsdPreCreditProcessServcie.class);

    // 产品信息
    @Autowired
    @Qualifier("prdInfoService")
    private PrdInfoService prdInfoService;

    // 客户经理名单
    @Autowired
    @Qualifier("ptProductUserService")
    private PtProductUserService ptProductUserService;

    // 预授信信息
    @Autowired
    @Qualifier("preNlsCreditInfoService")
    private PreNlsCreditInfoService preNlsCreditInfoService;

    // 授信申请信息表服务
    @Autowired
    @Qualifier("nlsCreditInfoService")
    private NlsCreditInfoService nlsCreditInfoService;

    // 告警服务
    @Autowired
    @Qualifier("nlsQueueSoltComnService")
    private NlsQueueSoltComnService nlsQueueSoltComnService;

    // 普税贷预核额
    @Autowired
    @Qualifier("psdPreCreditApprService")
    private PsdPreCreditApprService psdPreCreditApprService;

    // 额度信息
    @Autowired
    @Qualifier("lmtPrdContService")
    private LmtPrdContService lmtPrdContService;

    @Autowired
    @Qualifier("applicationThreadPool")
    private ApplicationThreadPool2 threadPool;

    @Autowired
    private PsdSendTaxRecordService psdSendTaxRecordService;

    @Autowired
    private PubSysInfoService pubSysInfoService;

    @Value("${tax.max.send.day:3}")
    private int maxSendDay;
}
